package com.enfo.zjabank.service;

import com.enfo.zjabank.dao.ChangeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhoum on 2016/8/24.
 */
@Service
public class ChangeServiceImpl implements ChangeService {
    @Autowired
    private ChangeDao changeDao;
    @Override
    @Transactional
    public void changeApply(List<Map<String, String>> form) throws Exception {
        String name;
        StringBuilder prpIds = new StringBuilder("''");
        Map<String,String> rebuildForm = new HashMap<>();
        for(Map<String,String> map:form){
            name = map.get("name");
            rebuildForm.put(name,map.get("value"));
            if(!"id,redirect_url,pntclsid".contains(name)){
                prpIds.append(",'"+name+"'");
            }
        }
        Map<String,String> columnNameMap = changeDao.getColumnNames(prpIds.toString());
        changeDao.addChange(rebuildForm,columnNameMap);
        changeDao.modifyState(rebuildForm);
    }
}
